package com.foreveross.action;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;
import com.foreveross.common.Exception.ExceptionConstant;
import com.foreveross.service.HouseLeaseService;
import com.foreveross.service.LogService;
import com.foreveross.util.AuthUtil;
import com.foreveross.util.FileUtil;
import com.foreveross.vo.HouseLease;
import com.foreveross.vo.common.FileVo;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;
/**
 * 房屋出租
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/HouseLeaseController")
public class SysHouseLeaseController  extends BaseCostroller {

	private Logger log = Logger.getLogger(SysHouseLeaseController.class);

	@Resource
	private HouseLeaseService houseLeaseService;

	@Resource
	private LogService logService;
	
	@RequestMapping(method = RequestMethod.POST, value = "/getHouseLeaseList")
	public void getHouseLeaseList(HttpServletRequest request,
			HttpServletResponse response,String housingArea,String houseId,
			String title,String rent,String text,
			String page,String rows,String minMoney,String maxMoney,String province,String userId) {
		ResultVo resultVo = new ResultVo();
		try {
			int total = (int)houseLeaseService.getHouseLeaseListCount(housingArea,title,houseId, minMoney, maxMoney,text,userId);
			PageVo<HouseLease> pageVo = new PageVo<HouseLease>(page,total,rows);
			List<HouseLease> list = houseLeaseService.getHouseLeaseList(housingArea,title,houseId, minMoney, maxMoney,text,pageVo.getpageIndex(),pageVo.getpageSize(),userId);
			pageVo.setRows(list);
			resultVo.setSuccess(true);
			resultVo.setData(pageVo);
			log.info("房屋出租 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("房屋出租  查看失败");
			log.error("房屋出租 查看失败!", e);
		}
		List listz = Arrays.asList("messageHouses","newsHouses","optimizedHouses");
		CommonUtil.PrintWriterResultWithJSON(response, resultVo,listz);
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/getHouseLeaseById")
	public void getHouseLeaseById(HttpServletRequest request,
			HttpServletResponse response,String id) {
		ResultVo resultVo = new ResultVo();
		try {
			HouseLease houseLease = houseLeaseService.getHouseLeaseById(id);
			List<FileVo> fileVoList = FileVo.getFileVoList(request, houseLease.getImage());
			resultVo.setOther(fileVoList);
			resultVo.setSuccess(true);
			resultVo.setData(houseLease);
			log.info("房屋出租 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("房屋出租  查看失败");
			log.error("房屋出租 查看失败!", e);
		}
		List listz = Arrays.asList("messageHouses","newsHouses","optimizedHouses");
		CommonUtil.PrintWriterResultWithJSON(response, resultVo,listz);
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/updateHouseLease")
	public void updateHouseLease(HttpServletRequest request,
			HttpServletResponse response, HouseLease houseLease,
			String updateType) {
		ResultVo resultVo = new ResultVo();
		try{
			houseLease.setImage(houseLease.getImage());
			if(updateType.equals(Constants.UPDATE_ADD)){
				houseLease.setCreationTime(new Date());
			}else if(updateType.equals(Constants.UPDATE_EDIT)){
				houseLease.setUpdateTime(new Date());
			}
			houseLeaseService.saveOrUpdate(houseLease, updateType);
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,updateType.equals(Constants.UPDATE_ADD)?Constants.LOG_ADD:Constants.LOG_UPDATE, "房屋出租"+Constants.getUpdateType(updateType),
					AuthUtil.getUserid(request), 
					Constants.getUpdateType(updateType)+"房屋出租[" + houseLease.toString()
							+ "]");
			log.info("房屋出租 "+updateType+"成功!");
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage("房屋出租 "+updateType+"失败");
			log.error("房屋出租"+updateType+"失败!",e);
		}
		
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	
	@RequestMapping(value = "/uploadHouseLeaseImg")
	public void uploadHouseLeaseImg(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("houseLeaseImg") MultipartFile file) {
		ResultVo resultVo = new ResultVo();
		try {
			resultVo = FileUtil.uploadFile(request, file, "/houseLease/", true);
			if (resultVo.isSuccess())
				resultVo.setData(CommonUtil.getHttpBasePath(request)
						+ resultVo.getData());
			else
				resultVo.setMessage(ExceptionConstant.getExceptionMessage((String) resultVo.getMessage())); 
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("上传文件失败");
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);

	}

	@RequestMapping(method = RequestMethod.POST, value = "/deleteHouseLease")
	public void deleteHouseLease(HttpServletRequest request,
			HttpServletResponse response, 
			String ids) {
		ResultVo resultVo = new ResultVo();
		try{
			houseLeaseService.deleteObjecList("HouseLease", ids);
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,Constants.LOG_DEL, "通知消息删除",
					AuthUtil.getUserid(request), 
					"房屋出租[" + ids+ "]");
			log.info("房屋出租 删除成功!");
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage("房屋出租 删除失败");
			log.info("房屋出租 删除失败!");
		}
		
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}


}
